﻿using System;
using System.Collections;
using System.Configuration;
using System.Web.Configuration;
using HospitalSystem.Core;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HospitalSystem
{
    public partial class servicerecord : System.Web.UI.Page
    {

        private findchildLog _fc;
        private GroupsLog _group;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            _fc = new findchildLog();
            _group = new GroupsLog();
        }

       
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
                   
        protected void CaseSelector3_CheckedChanged(object sender, EventArgs e)
        {
         

            _fc.BindDatadek(SelectStat,listchild,fname,lname);
         

            checkcaseno();
        }












        protected void sendsession()
        {
            Session["CID"] = SelectStat.Text;
            Session["FirstName"] = fname.Text;
            Session["LastName"] = lname.Text;


            _fc.Bindcase(SelectStat, caseno);
    

            Session["CaseNo"] = caseno.Text;
            _fc.creDetails(SelectStat,caseno,psnumber);
         Response.Redirect("~/servicerecord2.aspx");



        }

        protected void sendsessionpsnumber()
        {
            Session["PSNumber01"] = psnumber.Text;


        }

        
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            _fc.BindData(searchchild.Text, listchild);
        }
        
        protected void checkcaseno()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //ต้องการแก้เคสที่มีอยู่แล้ว
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT CID from Evaluation where CID = @sj", scon);
            cmd.Parameters.AddWithValue("@sj", SelectStat.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read() != false)
            {

                _group.crepsnumber(HostID, StaffID);
                _group.psnumbertolabel(psnumber);
                sendsessionpsnumber();
                sendsession();

            }

            else
            {
                Session["CID"] = SelectStat.Text;
                Session["FirstName"] = fname.Text;
                Session["LastName"] = lname.Text;
                MessageBox("เด็กรายได้ยังไม่ได้ทำการคัดกรองเราจะพาคุณไปหน้าทำการคัดกรองก่อนที่จะบันทึกการให้บริการ", "cal.aspx");

            }
            reader.Close();

        }
        private void MessageBox(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }



    }
}